package edu.nf.project.webs.storeOperation;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.nf.project.entitys.entity.storeOperation.convention.dish.Dish;
import edu.nf.project.entitys.entity.storeOperation.convention.dish.DishType;
import edu.nf.project.entitys.enums.storeOperation.DishStatusEnum;
import edu.nf.project.entitys.enums.storeOperation.DtNameEnum;
import edu.nf.project.services.exception.derivedAnomalies.DishException;
import edu.nf.project.services.storeOperation.DishService;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理控制器
 */
@RestController
@RequestMapping("/api/dishes")
public class DishController extends BaseController {
    @Autowired
    private DishService dishService;

    @GetMapping("/list")
    public ResultVO<List<Dish>> getDishList(){
        List<Dish> list = dishService.getDishList();
        System.out.println(list);
        return success(list);
    }

    /**
     * 获取菜品列表
     */
    @GetMapping
    public ResultVO<Page<Dish>> getDishList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String dishName,
            @RequestParam(required = false) Integer dtId) {
        try {
            Page<Dish> page = new Page<>(pageNum, pageSize);
            Page<Dish> result = dishService.getDishList(page, dishName, dtId);
            return success(result);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 获取菜品详细信息
     */
    @GetMapping("/{dishId}")
    public ResultVO<Dish> getDishInfo(@PathVariable Integer dishId) {
        try {
            Dish dish = dishService.getDishInfo(dishId);
            if (dish == null) {
                throw new DishException(40001, "菜品不存在");
            }
            return success(dish);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 创建新菜品
     */
    @PostMapping
    public ResultVO<Boolean> createDish(@RequestBody Dish dish) {
        try {
            boolean result = dishService.createDish(dish);
            return success(result);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 更新菜品信息
     */
    @PutMapping("/{dishId}")
    public ResultVO<Boolean> updateDish(
            @PathVariable Integer dishId,
            @RequestBody Dish dish) {
        try {
            boolean result = dishService.updateDish(dishId, dish);
            if (!result) {
                throw new DishException(40001, "菜品不存在");
            }
            return success(result);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 更新菜品状态
     */
    @PutMapping("/{dishId}/status")
    public ResultVO<Boolean> updateDishStatus(
            @PathVariable Integer dishId,
            @RequestParam DishStatusEnum status) {
        try {
            boolean result = dishService.updateDishStatus(dishId, status);
            if (!result) {
                throw new DishException(40001, "菜品不存在");
            }
            return success(result);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 获取菜品类型
     */
    @GetMapping("/{dishId}/type")
    public ResultVO<String> getDishType(@PathVariable Integer dishId) {
        try {
            String typeName = dishService.getDishType(dishId);
            if (typeName == null) {
                throw new DishException(40001, "菜品不存在");
            }
            return success(typeName);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 获取全部菜品类型
     */
    @GetMapping("/types")
    public ResultVO<String[]> getDishType() {
        try {
            String[] typeName = dishService.getDishTypes();
            if (typeName == null) {
                throw new DishException(40001, "菜品不存在");
            }
            return success(typeName);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 获取全部菜品类型对象
     */
    @GetMapping("/types/obj")
    public ResultVO<List<DishType>> getAllDishTypes() {
        try {
            List<DishType> obj = dishService.getAllDishTypes();
            return success(obj);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 删除某个菜品类型
     */
    @GetMapping("/types/del")
    public ResultVO<Boolean> getAllDishTypes(Integer dtId) {
        try {
            Boolean flag = dishService.delDishType(dtId);
            return success(flag);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 添加某个菜品类型
     * @return 是否成功
     */
    @PostMapping("/types/add")
    public ResultVO<Boolean> addDishType(@RequestParam String dt) {
        try {
            Boolean flag = dishService.addDishType(dt);
            return success(flag);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 修改某个菜品类型
     * @return 是否成功
     */
    @PostMapping("/types/upd")
    public ResultVO<Boolean> updDishType(@RequestParam Integer dt,@RequestParam String name) {
        try {
            Boolean flag = dishService.updDishType(dt, name);
            return success(flag);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            System.out.println(e);
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 获取全部菜品
     * @return 菜品对象集合
     */
    @GetMapping("/allDish")
    public ResultVO<List<Dish>> getAllDishObj() {
        try {
            List<Dish> list = dishService.getAllDish();
            return success(list);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 退菜
     * @param orderId 订单ID
     * @param dishId 菜品ID
     * @return 是否成功
     */
    @PostMapping("/return")
    public ResultVO<Boolean> returnDish(
            @RequestParam Integer orderId,
            @RequestParam Integer dishId) {
        try {
            Boolean result = dishService.returnDish(orderId, dishId);
            return success(result);
        } catch (Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            System.out.println(e);
            throw new DishException(40000, "系统错误，请稍后重试");
        }
    }
}